﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
using System.Data.SqlClient;

public partial class Teams_TeamProfile : System.Web.UI.Page
{
    private string id, autor;

    protected void Page_Load(object sender, EventArgs e)
    {
        MembershipUser User = Membership.GetUser(HttpContext.Current.User.Identity.Name);
        try
        {
            autor = User.UserName;
        }
        catch (Exception ex)
        {
            TextBox1.Visible = false;
            Button1.Visible = false;
            Label1.Visible = false;
        }
        id = Request.QueryString["t"];
        if (id == null)
        {
            Response.Redirect("~/Teams.aspx");
        }
        DataView druzyny = (DataView)dbDruzyny.Select(DataSourceSelectArguments.Empty);
        
        foreach (DataRowView drv in druzyny)
        {
            nazwaDruzyny.Text = drv["nazwa"].ToString();
            logo.ImageUrl = "ShowImage.ashx?id=" + id + "&type=team_logo";
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Teams.aspx");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        string commandText;
        DateTime date = new DateTime();
        commandText = @"INSERT INTO KOMNETARZE 
                        (id_druzyny, autor, data, tresc) VALUES (@id_druzyny, @autor, @data, @tresc)";
        try
        {

            using (SqlConnection connection = new SqlConnection(dbKomentarze.ConnectionString))
            {
                SqlCommand command = new SqlCommand(commandText, connection);

                command.Parameters.Add("@id_druzyny", SqlDbType.Int);
                command.Parameters["@id_druzyny"].Value = id;
                command.Parameters.Add("@autor", SqlDbType.NVarChar);
                command.Parameters["@autor"].Value = autor;
                command.Parameters.Add("@data", SqlDbType.DateTime);
                command.Parameters["@data"].Value = DateTime.Now;
                command.Parameters.Add("@tresc", SqlDbType.NVarChar);
                command.Parameters["@tresc"].Value = TextBox1.Text;

                connection.Open();
                command.ExecuteNonQuery();
                
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            throw;
        }

        Response.Redirect("~/TeamProfile.aspx?t=" + id);

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/PlayerProfile.aspx?action=new&id=1");
    }
    protected void Button3_PreRender(object sender, EventArgs e)
    {
        Button button = (Button)sender;
        
        String username = HttpContext.Current.User.Identity.Name;
        String[] user_roles = Roles.GetRolesForUser(username);

        button.Visible = false;
        foreach (string role in user_roles)
        {
            if (role.Equals("administrator portalu") || role.Equals("administrator rozgrywek ligowych"))
            {
                button.Visible = true;
                break;
            }
        }
    }
}
